package cn.e4j.modular.erp.controller;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.AppCxt;
import cn.e4j.modular.erp.service.Erp;
import cn.e4j.modular.erp.service.impl.E10ServiceImpl;
import cn.e4j.modular.erp.service.impl.GPServiceImpl;
import cn.e4j.modular.erp.service.impl.T100ServiceImpl;
import cn.e4j.modular.erp.service.impl.YFServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by ganjia on 2019-09-18.
 */
@Service
public class ErpLogic {

    @Autowired
    private E4jProperties e4jProperties;

    @Autowired
    private T100ServiceImpl t100ServiceImpl;

    @Autowired
    private GPServiceImpl gpServiceImpl;

    @Autowired
    private E10ServiceImpl e10ServiceImpl;

    @Autowired
    private YFServiceImpl yfServiceImpl;


    public ErpLogic(){

    }

    /**
     * 根据产品线获取对应的方法
     * fun 方法名称
     * dto 传入方法的参数
     * @param fun
     * @param dto
     * @param <T>
     * @return
     * @throws NoSuchMethodException
     */
    public <T> T getERPService(String fun,Dto dto) throws NoSuchMethodException {
        Method method = null;
        T result = null;
        try {
            switch(AppCxt.getParam("ERP")){
                case "T100":
                    method = t100ServiceImpl.getClass().getMethod(fun, Dto.class);
                    return (T) method.invoke(t100ServiceImpl,dto);
                case "GP":
                    method = gpServiceImpl.getClass().getMethod(fun, Dto.class);
                    return (T) method.invoke(gpServiceImpl,dto);
                case "E10":
                    method = e10ServiceImpl.getClass().getMethod(fun, Dto.class);
                    return (T) method.invoke(e10ServiceImpl,dto);
                case "YF":
                    method = yfServiceImpl.getClass().getMethod(fun, Dto.class);
                    return (T) method.invoke(yfServiceImpl,dto);
                default:
                    method = gpServiceImpl.getClass().getMethod(fun, Dto.class);
                    return (T) method.invoke(gpServiceImpl,dto);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public Erp getErpService(){
//        switch(e4jProperties.getErpVersion()){
        switch(AppCxt.getParam("ERP")){
            case "T100":
                return t100ServiceImpl;
            case "GP":
                return gpServiceImpl;
            /*case "E10":
                return e10ServiceImpl;
            case "YF":
                return yfServiceImpl;*/
            default:
                return gpServiceImpl;
        }
    }
}
